using MicroMsg.Common.Event;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
namespace MicroMsg.UI.Page
{
	public class RegStepSetUserName : BasePage
	{
		private EventWatcher mWatcherQQRegNickEvent;
		private EventWatcher mWatcherPhoneReg;
		private LOGINTYPE mLoginType;
		private string mStrPassFromPhoneReg;
		internal Storyboard Storyboard1;
		internal Grid LayoutRoot;
		internal Grid sencondRoot;
		internal panelTitle2Line panelTitle;
		internal StackPanel ContentPanel;
		internal PhoneTextBox textBoxNickName;
		internal TextBlock textBlock1;
		private bool _contentLoaded;
		public RegStepSetUserName()
		{
			this.InitializeComponent();
			this.SetAppBarWording();
		}
		private void textBoxNickNameGotFocus(object sender, RoutedEventArgs e)
		{
		}
		private void textBoxNickNameTextChanged(object sender, RoutedEventArgs e)
		{
			if (this.textBoxNickName.get_Text().get_Length() != 0)
			{
				(base.get_ApplicationBar().get_Buttons().get_Item(0) as ApplicationBarIconButton).set_IsEnabled(true);
				return;
			}
			(base.get_ApplicationBar().get_Buttons().get_Item(0) as ApplicationBarIconButton).set_IsEnabled(false);
		}
		private void inputTxt_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.get_Key() == 3 && sender == this.textBoxNickName)
			{
				this.btnNextStep_Click(null, null);
				e.set_Handled(true);
			}
		}
		private void btnPreviousStep_Click(object sender, EventArgs e)
		{
			base.GoBack();
		}
		private void btnNextStep_Click(object sender, EventArgs e)
		{
			base.Focus();
			if (ToolFunction.getStrDisLen(this.textBoxNickName.get_Text()) > 32)
			{
				MessageBoxManager.ShowToast(strings.Reg_PhoneName_MsgBadName, 2500, null, false);
				return;
			}
			this.disableAllControls();
			switch (this.mLoginType)
			{
			case LOGINTYPE.REG_TYPE_QQ:
				this.setUserNameRegByQQ();
				return;
			case LOGINTYPE.REG_TYPE_MAIL:
				this.setUserNameRegByMail();
				return;
			case LOGINTYPE.REG_TYPE_PHONE:
				this.setUserNameRegByPhone();
				return;
			default:
				return;
			}
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null && object_0.Length != 0)
			{
				LoginKind loginKind = object_0[0] as LoginKind;
				this.mLoginType = loginKind.mLoginType;
				if (this.mLoginType == LOGINTYPE.REG_TYPE_MAIL)
				{
					PageManager.getInstance().PopAllPages(true);
				}
				else
				{
					if (this.mLoginType == LOGINTYPE.REG_TYPE_PHONE && 2 == object_0.Length)
					{
						this.mStrPassFromPhoneReg = (object_0[1] as string);
					}
				}
				this.registerEventHandler();
			}
		}
		protected override void OnPageOut()
		{
			if (this.mWatcherQQRegNickEvent != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_FILLUSERNAME_SUCCESS, this.mWatcherQQRegNickEvent);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_NEWREG_SUCCESS, this.mWatcherQQRegNickEvent);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_NEWREG_ERR, this.mWatcherQQRegNickEvent);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_FILLUSERNAME_ERR, this.mWatcherQQRegNickEvent);
				this.mWatcherQQRegNickEvent = null;
			}
			if (this.mWatcherPhoneReg != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_MOBILEREG_SUCCESS, this.mWatcherPhoneReg);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_MOBILEREG_ERR, this.mWatcherPhoneReg);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_SUCCESS, this.mWatcherPhoneReg);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_FAIL, this.mWatcherPhoneReg);
				EventCenter.removeEventWatcher(EventConst.ON_SYNC_CONTACT_COMPLETE, this.mWatcherPhoneReg);
				EventCenter.removeEventWatcher(EventConst.ON_SYNC_CONTACT_FAIL, this.mWatcherPhoneReg);
				this.mWatcherPhoneReg = null;
			}
		}
		private void registerEventHandler()
		{
			switch (this.mLoginType)
			{
			case LOGINTYPE.REG_TYPE_QQ:
				if (this.mWatcherQQRegNickEvent == null)
				{
					this.mWatcherQQRegNickEvent = new EventWatcher(this, null, new EventHandlerDelegate(this.setQQRegNickHandler));
					EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_FILLUSERNAME_SUCCESS, this.mWatcherQQRegNickEvent);
					EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_NEWREG_SUCCESS, this.mWatcherQQRegNickEvent);
					EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_NEWREG_ERR, this.mWatcherQQRegNickEvent);
					EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_FILLUSERNAME_ERR, this.mWatcherQQRegNickEvent);
					return;
				}
				break;
			case LOGINTYPE.REG_TYPE_MAIL:
				break;
			case LOGINTYPE.REG_TYPE_PHONE:
				this.mWatcherPhoneReg = new EventWatcher(this, null, new EventHandlerDelegate(this.onPhoneAuthRegHandler));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_MOBILEREG_SUCCESS, this.mWatcherPhoneReg);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_MOBILEREG_ERR, this.mWatcherPhoneReg);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_SUCCESS, this.mWatcherPhoneReg);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_FAIL, this.mWatcherPhoneReg);
				EventCenter.registerEventWatcher(EventConst.ON_SYNC_CONTACT_COMPLETE, this.mWatcherPhoneReg);
				EventCenter.registerEventWatcher(EventConst.ON_SYNC_CONTACT_FAIL, this.mWatcherPhoneReg);
				break;
			default:
				return;
			}
		}
		private void setUserNameRegByQQ()
		{
			this.disableAllControls();
			ServiceCenter.sceneGetUserName.doScene(this.textBoxNickName.get_Text());
		}
		private void setUserNameRegByMail()
		{
			PageManager.getInstance().PopAllPages(true);
			ConfigMgr.setBool("KeyBJustRegUser", true);
			MainPage.GotoThisPage(new object[]
			{
				true
			}, true);
		}
		private void setUserNameRegByPhone()
		{
			ServiceCenter.sceneNewReg.doSceneSetPwdforMobileReg(this.mStrPassFromPhoneReg, this.textBoxNickName.get_Text());
		}
		private void setQQRegNickHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			this.enableAllControls();
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_FILLUSERNAME_SUCCESS)
			{
				this.disableAllControls();
				ServiceCenter.sceneNewReg.doScene();
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_NEWREG_SUCCESS)
			{
				PageManager.getInstance().PopAllPages(true);
				GConfigMgr.settings.strLastLoginName = LoginMainView.sStrNameToLogin;
				LoginMainView.sStrNameToLogin = null;
				GConfigMgr.saveSetting();
				ConfigMgr.setBool("KeyBJustRegUser", true);
				MainPage.GotoThisPage(new object[]
				{
					true
				}, true);
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_NEWREG_ERR)
			{
				MMMessageBox.Show(strings.Reg_PhoneName_MsgRegFail, true, "确定");
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_FILLUSERNAME_ERR)
			{
				RetConst retConst = (RetConst)evtArgs.mObject;
				if (RetConst.MM_ERR_NICKRESERVED != retConst)
				{
					if (RetConst.MM_ERR_NICKNAMEINVALID != retConst)
					{
						if (RetConst.MM_ERR_CLIENT == retConst)
						{
							MMMessageBox.Show(strings.Reg_PhoneName_MsgSetNickFailed, strings.Reg_PhoneName_MsgBadNet, 1, true, strings.COM_Confirm, "取消");
							return;
						}
						MMMessageBox.Show(strings.Reg_PhoneName_MsgIllegalNick, strings.Reg_PhoneName_MsgBadNick, 1, true, strings.COM_Confirm, "取消");
						return;
					}
				}
				MMMessageBox.Show(strings.Reg_PhoneName_MsgIllegalNick, strings.Reg_PhoneName_MsgBadNick, 1, true, strings.COM_Confirm, "取消");
				return;
			}
		}
		private void onPhoneAuthRegHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			this.enableAllControls();
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_MOBILEREG_SUCCESS)
			{
				PageManager.getInstance().PopAllPages(true);
				GConfigMgr.settings.strLastLoginName = RegByPhoneStepInputNum.sStrNameToLogin;
				RegByPhoneStepInputNum.sStrNameToLogin = null;
				GConfigMgr.saveSetting();
				this.askEnableMConMatching();
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_MOBILEREG_ERR)
			{
				RetConst retConst = (RetConst)evtArgs.mObject;
				if (RetConst.MM_ERR_NICKRESERVED != retConst)
				{
					if (RetConst.MM_ERR_NICKNAMEINVALID != retConst)
					{
						if (RetConst.MM_ERR_CLIENT == retConst)
						{
							MMMessageBox.Show(strings.Reg_PhoneName_MsgSetNickFailed, strings.Reg_PhoneName_MsgBadNet, 1, true, strings.COM_Confirm, "取消");
							return;
						}
						MMMessageBox.Show(strings.Reg_PhoneName_MsgIllegalNick, strings.Reg_PhoneName_MsgBadNick, 1, true, strings.COM_Confirm, "取消");
						return;
					}
				}
				MMMessageBox.Show(strings.Reg_PhoneName_MsgSetNickFailed, strings.Reg_PhoneName_MsgBadNick, 1, true, strings.COM_Confirm, "取消");
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_GETFRIEND_SUCCESS || evtArgs.mEventID == EventConst.ON_NETSCENE_GETFRIEND_FAIL || evtArgs.mEventID == EventConst.ON_SYNC_CONTACT_FAIL)
			{
				ConfigMgr.setBool("KeyBJustRegUser", true);
				MainPage.GotoThisPage(new object[]
				{
					true
				}, true);
			}
		}
		private void enableAllControls()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
			(base.get_ApplicationBar().get_Buttons().get_Item(0) as ApplicationBarIconButton).set_IsEnabled(true);
		}
		private void disableAllControls()
		{
			ProcessingCover.getProgressBar(this.LayoutRoot, null, null, false, false, null);
			(base.get_ApplicationBar().get_Buttons().get_Item(0) as ApplicationBarIconButton).set_IsEnabled(false);
		}
		public void SetAppBarWording()
		{
			if (base.get_ApplicationBar() != null && 1 == base.get_ApplicationBar().get_Buttons().get_Count())
			{
				ApplicationBarIconButton applicationBarIconButton = (ApplicationBarIconButton)base.get_ApplicationBar().get_Buttons().get_Item(0);
				applicationBarIconButton.set_Text(strings.PCon_Bind_BtnComplete);
			}
		}
		private void askEnableMConMatching()
		{
			MMMessageBox mMMessageBox = MMMessageBox.Show(strings.Login_Main_MsgAskMobileTitle, strings.Login_Main_MsgAskMobileCon, true, strings.COM_Confirm, strings.COM_Cancel);
			mMMessageBox.ReturnEvent = new DlgReturnEvent(this.askEnableMConMatchingAlertReturn);
		}
		private void askEnableMConMatchingAlertReturn(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
				ConfigMgr.setBool("bIsEnabledAddrBookMatch", true);
				this.startAddrBookMatching();
				return;
			case 1:
				ConfigMgr.setBool("KeyBJustRegUser", true);
				MainPage.GotoThisPage(new object[]
				{
					true
				}, true);
				return;
			default:
				ConfigMgr.setBool("KeyBJustRegUser", true);
				MainPage.GotoThisPage(new object[]
				{
					true
				}, true);
				return;
			}
		}
		private void startAddrBookMatching()
		{
			ServiceCenter.mobileContactMgr.syncAndUploadContactFromDevice(new DelegateProgressBar(this.setProgressBar));
		}
		public void setProgressBar(string strTitleProgressBar)
		{
			if (strTitleProgressBar == null)
			{
				return;
			}
			ProcessingCover.setProgressBarTitle(this.LayoutRoot, strings.PCon_List_MsgSncing, null, false, false, null);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/LoginViews/RegStepSetUserName.xaml", 2));
			this.Storyboard1 = (Storyboard)base.FindName("Storyboard1");
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.sencondRoot = (Grid)base.FindName("sencondRoot");
			this.panelTitle = (panelTitle2Line)base.FindName("panelTitle");
			this.ContentPanel = (StackPanel)base.FindName("ContentPanel");
			this.textBoxNickName = (PhoneTextBox)base.FindName("textBoxNickName");
			this.textBlock1 = (TextBlock)base.FindName("textBlock1");
		}
	}
}
